package action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import metier.Imetier;
import model.Role;
import model.User;

@SuppressWarnings("serial")
public class WelcomeUserAction extends ActionSupport {

	@Autowired
	private Imetier metier;

	private Long rol;
	private Long pk;
	private String nom;
	private String email;
	private String login;
	private String password;
	static private String v;
	
	private List<Role> role = new ArrayList<Role>();
	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	User u = new User();
	List<User> user = new ArrayList<User>();

	public Long getRol() {
		return rol;
	}

	public void setRol(Long rol) {
		this.rol = rol;
	}

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public List<Role> getRole() {
		return role;
	}

	public void setRole(List<Role> role) {
		this.role = role;
	}

	public Imetier getMetier() {
		return metier;
	}

	public void setMetier(Imetier metier) {
		this.metier = metier;
	}

	public Long getPk() {
		return pk;
	}

	public void setPk(Long pk) {
		this.pk = pk;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getV() {
		return v;
	}

	public void setV(String V) {
		v = V;
	}

	public List<User> getUser() {
		return user;
	}

	public void setUser(List<User> user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return super.execute();
	}

	// =================================================

	public String addUser() {

		String s = "";

		User a = metier.findUserByLogin(login);

		if (a.getLogin() != null) {

			s = "error";
			addActionError("login existe");
		} else {
			Role rol1 = metier.findRollbyId(1L);
			metier.addUser(new User(nom, email, login, password, rol1));
			s = "success";
		}
		role = metier.findALLRoll();
		return s;
	}

	public String findAllUser() {

		user = metier.findAllUser();
		return "success";
	}

	// ====================================================

	public String updateUser() {
		String s = "";

		User a = metier.findUserByLogin(login);

		if (a.getLogin() != null) {
			if (!a.getLogin().equals(v)) {
				s = "error";
				addActionError("login existe");

			}
			if (a.getLogin().equals(v)) {
				metier.updateUser(new User( nom, email, login, password));

				s = "success";
			}
		} else {
			metier.updateUser(new User( nom, email, login, password));

			s = "success";
		}

		login = "";
		password = "";
		nom = "";
		email = "";
		user = metier.findAllUser();
		return s;
	}

	// ==============================================
	public String abonnementsParType() {
		user = metier.findAllUser();
		u = metier.findUserById(pk);
		login = u.getLogin();
		password = u.getPassword();
		nom = u.getNom();
		email = u.getEmail();
		v = login;
		return "success";
	}

	// =======================================================
	public String deleteuser() {

		metier.deleteUser(login);
		user = metier.findAllUser();
		return "success";
	}

	// ==========================================================
	public String authentifier() {
		String s = "";
		User a = metier.findUserByLogin(login);
		if (a.getLogin() != null) {
			if ((a.getPassword().equals(password))&&(a.getRole().getId_role()==1)) {
				session.put("connecte", "true");
				session.put("login", a.getLogin());
				//session.put("id", a.getLogi());
				s = "success";

			} else {
				s = "error";
				
			}
		} else {
			s = "error";
			
		}
		login = "";
		password = "";
		return s;

	}

	// --------------------------------------------------------------

	public String DeletSession() {
		// session = ActionContext.getContext().getSession();
		session.clear();
		return "success";
	}

	// ==================================================================

	public String findAllRoll() {
		role = metier.findALLRoll();

		return "success";

	}
	//================================================================
	public String findUserById(){
		u=metier.findUserByLogin(login);
		return "success";
	}
	//--------------------------------------------------
	public String findAlluser(){
		user=metier.findAllUser();
		
	findAllRoll();
		return "success";
	}
//-----------------------------------------------------------------
	public String authentifieradmin() {
		String s = "";
		User a = metier.findUserByLogin(login);
		if (a.getLogin() != null) {
			if ((a.getPassword().equals(password)) && (a.getRole().getId_role()==2)) {
				
				session.put("connecte", "true");
				session.put("login", a.getLogin());
				//session.put("id", a.getLogi());
				
				s = "success";
     
			} else {
				s = "error";
				addActionError("login ou le mot passe est incorect");

			}
		} else {
			s = "error";
			addActionError("login ou le mot passe est incorect");
		}
		login = "";
		password = "";
		System.out.println(s);
		return s;

	}

}